package org.hefan.pet.service.impl;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.hefan.basic.service.impl.BaseServiceImpl;
import org.hefan.basic.util.LoginContextUtil;
import org.hefan.pet.domain.Pet;
import org.hefan.pet.domain.PetDetail;
import org.hefan.pet.mapper.PetDetailMapper;
import org.hefan.pet.mapper.PetMapper;
import org.hefan.pet.service.IPetService;
import org.hefan.user.domain.Logininfo;
import org.hefan.user.domain.User;
import org.hefan.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public void save(Pet pet) {
        // 先保存宠物,返回自增长的主键id
        // 添加宠物信息
        super.save(pet);
        // 通过pet得到明细detail,把pet自增长返回的主键id设置到明细detail里,再保存detail
        // 添加明细
        PetDetail detail = pet.getDetail();
        if (detail != null) {
            detail.setPet_id(pet.getId());
            petDetailMapper.save(detail);
        }
    }

    @Override
    @Transactional
    public void remove(Long id) {
        // 先删除宠物
        super.remove(id);
        // 再删除宠物明细
        petDetailMapper.removeByPetId(id);
    }

    @Override
    @Transactional
    public void batchRemove(List<Long> ids) {
        // 先删除宠物
        super.batchRemove(ids);
        // 再删除宠物明细
        petDetailMapper.batchRemoveByPetIds(ids);
    }

    @Override
    @Transactional
    public void update(Pet pet) {
        // 先修改宠物信息
        super.update(pet);
        // 再修改宠物明细
        PetDetail detail = pet.getDetail();
        if (detail != null) {
            detail.setPet_id(pet.getId());
            petDetailMapper.updateByPetId(detail);
        }
    }

    /**
     * 通过map传两个参数
     * @param ids
     */
    @Override
    public void onsale(List<Long> ids) {
        Map<String,Object> map = new HashMap<>();
        map.put("ids",ids);
        // 设置上架时间onsaletime = new Date();
        map.put("onsaletime",new Date());
        // 修改state状态 = 1 xml写死了
        petMapper.onsale(map);
    }

    @Override
    public void offsale(List<Long> ids) {
        // 修改state状态 = 0
        Map<String,Object> map = new HashMap<>();
        map.put("ids",ids);
        // 设置下架时间offsaletime= new Date();
        map.put("offsaletime",new Date());
        petMapper.offSale(map);
    }

    @Override
    public void adopt(Long id, HttpServletRequest request) {
        Pet pet = petMapper.loadById(id);
        //1.下架
        pet.setState(0);
        //2.设置下架时间
        pet.setOffsaletime(new Date());
        //3.绑定用户id
        Logininfo logininfo = LoginContextUtil.getLogininfo(request);
        User user = userMapper.findByLogininfoId(logininfo.getId());
        pet.setUser_id(user.getId());
        pet.setUser(user);
        //修改新的状态
        petMapper.update(pet);
        //4.生成订单 @TODO
        //5.支付 @TODO
    }
}
